package com.example.adapter;

import java.io.IOException;
import java.util.concurrent.ThreadFactory;

import com.example.music.MainActivity;
import com.example.music.MusicPlayActivity;
import com.xiang.data.Contant;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
import android.os.Message;
import android.widget.Toast;

public class MusicControl extends Service implements
		MediaPlayer.OnCompletionListener {
	public static MediaPlayer mMediaPlayer;
	Context context;
	int MSG; 
	public static int MUSIC_LOADING = 1;
	public static int MUSIC_CONTROL = 2;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		if (mMediaPlayer != null) {
			mMediaPlayer.reset();
			mMediaPlayer.release();
			mMediaPlayer = null;
		}
		mMediaPlayer = new MediaPlayer();
		/* 监听播放是否完成 */
		mMediaPlayer.setOnCompletionListener(this);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		/* 得到从startService传来的动作，后是默认参数，这里是我自定义的常量 */
		MSG = intent.getIntExtra("control", MUSIC_LOADING);
		if (MSG == MUSIC_LOADING) {
			playMusic();
		}
		if (MSG == MUSIC_CONTROL) {
			if (mMediaPlayer.isPlaying()) {// 正在播放
				mMediaPlayer.pause();// 暂停
			} else {// 没有播放
				mMediaPlayer.start();
			}
		}
		MainActivity.getAdapter().resetData(MainActivity.fileList);
		MainActivity.getAdapter().notifyDataSetChanged();
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mMediaPlayer != null) {
			mMediaPlayer.stop();
			mMediaPlayer.release();
			mMediaPlayer = null;
		}
	}

	public void playMusic() {
		try {
			/* 重置多媒体 */
			mMediaPlayer.reset();
			/* 读取mp3文件 */
			if(MainActivity.filePath == null){
				Message msg = new Message();
				msg.what = Contant.MUSICPATH_NULL;
				MainActivity.handler.sendMessage(msg);
				return;
			}
			mMediaPlayer.setDataSource(MainActivity.filePath);
//			mMediaPlayer.setDataSource("http://stream19.music.soso.com/30436088.mp3");
			

			// Uri uri =
			// Uri.parse(MUSIC_PATH+TestMediaPlayer.mMusicList.get(TestMediaPlayer.currentListItme));
			// mMediaPlayer.create(PlayerService.this,uri);
			/* 准备播放 */
			mMediaPlayer.prepare();
			/* 开始播放 */
			mMediaPlayer.start();
			/* 是否单曲循环 */
			mMediaPlayer.setLooping(false);

			// 开启刷新
			if(MusicPlayActivity.seekBar!=null){
				refreshSeekbarThread thread = new refreshSeekbarThread();
				thread.start();
			}
			
		} catch (IOException e) {
		}
	}

	/**
	 * 播放完当前歌曲，自动播放下一首
	 */
	@Override
	public void onCompletion(MediaPlayer mp) {
		if (MainActivity.musicNum >= MainActivity.fileList.size()) {
			Toast.makeText(this, "已到最后一首歌曲", Toast.LENGTH_SHORT).show();
		} else {
			MainActivity.musicNum++;
			MainActivity.filePath = (String) MainActivity.fileList.get(
					MainActivity.musicNum).get("filepath");
			playMusic();
		}
	}

	/**
	 * 刷新进进度条
	 * 
	 * @author Administrator
	 * 
	 */
	class refreshSeekbarThread extends Thread {
		@Override
		public void run() {
			int CurrentPosition = 0;// 设置默认进度条当前位置
			int total = mMediaPlayer.getDuration();//
			while (mMediaPlayer != null && CurrentPosition < total) {
				try {
					Thread.sleep(1000);
					if (mMediaPlayer != null) {
						CurrentPosition = mMediaPlayer.getCurrentPosition();
					}
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				MusicPlayActivity.seekBar.setProgress(CurrentPosition);
			}
		}
	}

}
